#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "cgic.h"
#include <mqueue.h>
#include <sys/msg.h>

#define MSG_NAME "/msg_file_vdec"

typedef struct
{
    long mtype;
    int data;
} Message;

Message message;

int cgiMain()
{
	key_t key_msg;
	int msgid;

	key_msg = ftok(MSG_NAME, 'k');

	msgid = msgget(key_msg, 0666 | O_RDONLY);
	printf("msgid = %d\n", msgid);

	cgiHeaderContentType("text/html");
	fprintf(cgiOut, "<HTML><HEAD>\n");
	fprintf(cgiOut, "<TITLE>Start Push</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY>");
	fprintf(cgiOut, "<H2>Start Push</H2>");
	fprintf(cgiOut, "<a href=\"/cgi-bin/index_button.cgi\">To Main Page</a>\n");
	fprintf(cgiOut, "</BODY>\n");
	fprintf(cgiOut, "</HTML>\n");

	// 向 get.c 发送消息：0
    message.mtype = 1;
    message.data = 5;

    if (msgsnd(msgid, &message, sizeof(int), 0) == -1)
    {
        fprintf(cgiOut, "<H2>ERROR SEND QUEUE</H2>");
        //exit(1);
    }
    else
    {
        fprintf(cgiOut, "<H2>SUCCESS SEND QUEUE</H2>");
    }

	// write(fd, "1", strlen("1"));
	// close(fd);

	return 0;
}
